import sys

import demjson

import AM
import Conn
import DD
import JD

def updateRecord(userID):
    conn = Conn.getConn()
    cursor = conn.cursor()
    cursor.execute(
        "SELECT ID,JDURL,DDURL,AMURL FROM TB_PRODUCT WHERE USER_ID="+str(userID))
    results = cursor.fetchall()
    for row in results:
        print("XXXXXXX")
        ID=row[0]
        JDURL=row[1]
        DDURL=row[2]
        AMURL=row[3]

        recordSQL="INSERT INTO TB_PRODUCT_RECORD (PRODUCT_ID"
        valueSQL="('"+str(ID)+"'"

        JDprice=JD.doGetPrice(JDURL)
        if (JDprice!=None):
            recordSQL = recordSQL + ",JD_price"
            valueSQL = valueSQL + ",'" + str(JDprice) + "'"

        DDprice=DD.doGetPrice(DDURL)
        if (DDprice != None):
            recordSQL = recordSQL + ",DD_price"
            valueSQL = valueSQL + ",'" + str(DDprice) + "'"

        AMprice=AM.getPrice(AMURL)
        if (AMprice != None):
            recordSQL = recordSQL + ",AM_price"
            valueSQL = valueSQL + ",'" + str(AMprice) + "'"

        recordSQL = recordSQL + ",Date) "
        valueSQL = valueSQL + ",now()) "

        SQL = recordSQL + " VALUES " + valueSQL
        print(SQL)
        cursor.execute(SQL)
        conn.commit()

    conn.close()


json=sys.argv[1]
data=demjson.decode(json)
userID=data['USERID']
updateRecord(userID)